//Linked List
#include <iostream>
#include "stdio.h"
#include "stdlib.h"

#define LEN sizeof (struct linlist)

struct linlist
{
	int data;
	struct linlist *link;
};

typedef struct linlist linlist;

void creat(linlist *top);
void insert(linlist *top);
void dlt(linlist *top);
void floc(linlist *top);
void plist(linlist *top);

int main()
{
	int cho;

start:
	linlist *top;
	top=(linlist*)malloc(LEN);
	creat(top);
	printf ("\nYour Linkde List is:\n");
	plist(top);
	getchar();

choose:
	printf ("\n1. Insert a number in to this list.\n");
	printf ("2. Delete a number from this list.\n");
	printf ("3. Find a number if it in this list.\n");
	printf ("4. Print this list.\n");
	printf ("5. Creat a new list.");
	printf ("\nPlease choose what Function which you want:");
	scanf ("%d",&cho);
	switch (cho)
	{
	case 1 :
		insert(top);
		break;
	case 2 :
		dlt(top);
		break;
	case 3 :
		floc(top);
		break;
	case 4 :
		printf ("\nYour list is:\n");
		plist(top);
		break;
	case 5 :
		printf ("\n");
		goto start;
		break;
	default :
		printf ("There have no choose which number is:%d.\n",cho);
		break;
	}

	getchar();
	goto choose;
	return 0;
}

void creat(linlist *top)
{
	linlist *p,*s;
	int num,cho;
	char ch;

	(*top).link=NULL;
	p=top;

	printf ("Which way do you want to creat this list?\n");
	printf ("1. By Positive sequence;\n");
	printf ("2. By Inverted order.\n");
	printf ("Please choose a way to creat this list:");
	scanf ("%d",&cho);

	switch (cho)
	{
	case 1:
		printf ("Pelase input a List.(End with input 0)\n");
		scanf ("%d%c",&num,&ch);

		while (num)
		{
			s=(linlist*)malloc(LEN);
			(*p).link=s;
			(*s).data=num;
			(*s).link=NULL;
			p=s;
			scanf("%d%c",&num,&ch);
		}
		break;
	case 2:
		printf("Pelase input a List.(End with input 0)\n");
		scanf("%d%c",&num);
		while(num)
		{
			s=(linlist*)malloc(LEN);
			(*s).data=num;
			(*s).link=(*p).link;
			(*p).link=s;
			scanf("%d%c",&num,&ch);
		}
		break;
	}
}

void insert(linlist *top)
{
	int i,num,loc;
	linlist *p,*s,*temp;

	printf ("\nIf you want Insert a number,please input it:");
	scanf ("%d",&num);
	printf ("Please input which Location are you want to Insert:");
	scanf ("%d",&loc);
	p=top;

	for (i=0;;i++)
	{
		if (p==NULL)
		{
			printf ("\nThe sequence list is full,you can't insert number.\n");
			break;
		}
		if (i==loc-1)
		{
			s=(linlist*)malloc(LEN);
			(*s).data=num;
			temp=(*p).link;
			(*p).link=s;
			(*s).link=temp;
			break;
		}
		p=(*p).link;
	}
	printf ("\nThe new list is:\n");
	plist(top);
}

void dlt(linlist *top)
{
	int num;
	linlist *p,*s;

	printf ("\nIf you want Delete a number,please input it:");
	scanf ("%d",&num);
	p=(*top).link;

	while (p)
	{
		if ((*(*p).link).data==num)
		{
			s=(*p).link;
			(*p).link=(*s).link;
			free(s);
			break;
		}
		p=(*p).link;
	    if (p==NULL)
		{
		    printf("Sorry,this list have not number %d.\n",num);
		    break;
		}
	}
	printf ("\nThe new list is:\n");
	plist(top);
}

void plist(linlist *top)
{
	linlist *p;
	p=(*top).link;

	while (p)
	{
		printf (" %d ",(*p).data);
		p=(*p).link;
	}
	printf ("\n");
}

void floc(linlist *top)
{
	int num;
	int i=1;
	linlist *p;
	p=(*top).link;
	printf ("\nIf you want Find a number,please input it:");
	scanf ("%d",&num);
	while (p&&(*p).data!=num)
	{
		p=(*p).link;
		i++;
	}
	printf ("In this list,the number's(%d) location is:%d\n",num,i);
}

